import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, ModalService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { GlobalService } from '../../../../cores/services';
import { InventoryService } from '../../services/inventory.service';
import { Inventory } from '../../interfaces/inventory.interfaces';
import { skipWhile, switchMap } from 'rxjs/operators';
import { ModalCostComponent } from './modalcost.component';

@Component({
    templateUrl: './inventory.component.html',
})
export class InventoryComponent implements OnInit {


    theads = ['#', '期初单位', '仓库名称', '期初日期', '商品品种数', '商品总箱数', '录入人 ', '操作'];

    theadtwo = ['#', '商品名称', '商品条码', '口味/颜色', '规格/尺码', '基本单位', '成本价 ', '期初数量', '期初箱数', '库存金额', '生产日期', ' 到期日', '库位号', '操作'];
    search = new SearchParams({ start: '', end: '' });

    list = new Array<Inventory>();
    loading = false;
    inventory: Inventory = { id: 0, beginOfPeriodDate: '' };
    pagination = new Pagination();
    isSelect = false;
    idDetail: any = '';
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        public global: GlobalService,
        private confirm: ConfirmService,
        private inventoryService: InventoryService,
        private toast: ToastService,
        private modal: ModalService,
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/admin/company'))
            .subscribe(() => this.loadDatas());
        this.loadDatas();
    }

    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }

    loadDatas() {
        this.loading = true;
        this.inventoryService.searchCompany(this.pagination, this.search).subscribe({
            next: res => {
                this.pagination.total = res.datas.total;
                this.list = res.datas.rows;
                res.datas.rows.forEach(item => {
                    item['isSelect'] = this.isSelect;
                });
            },
            complete: () => this.loading = false
        });
    }
    /**
* order查询商品详情
*/
    // detailLoad(detailId: number) {
    //     this.idDetail = detailId;
    //     this.purchaseService.getDetail(this.idDetail)
    //         .subscribe({
    //             next: res => {
    //                 this.detailList = res.datas;
    //                 this.list.forEach((v, i) => {
    //                     if (v.id === this.idDetail) {
    //                         this.list[i].isSelect = true;
    //                     } else {
    //                         this.list[i].isSelect = false;
    //                     }
    //                 });

    //             },
    //             complete: () => this.loading = false
    //         });
    // }

    showEditModal(item: Inventory) {
        const modal = this.modal.create(ModalCostComponent, { center: true });
        modal.instance.inventory = Object.assign(modal.instance.inventory, item);
        modal.open().subscribe(() => {
            this.toast.success('保存成功', '成功保存库存');
            this.loadDatas();
        });
    }
    confirmDelete(id: number) {
        this.confirm.danger('删除确认', `您确认删除库存'吗？`)
            .pipe(switchMap<void, ApiData>(() => this.inventoryService.deleteinventory(id)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(this.inventory), 1);
                this.toast.success('删除成功', `成功删除库存'`);
                this.loadDatas();
            });
    }
}
